package ${sourceBasePackage}.controller;

import com.bidb.core.controller.BaseEntityController;
import com.bidb.core.dto.ResultData;
import com.bidb.core.dto.serach.PageResult;
import com.bidb.core.service.BaseEntityService;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ${sourceBasePackage}.api.${entityName}Api;
import ${sourceBasePackage}.dto.${entityName}Dto;
import ${sourceBasePackage}.dto.${entityName}SearchDto;
import ${sourceBasePackage}.entity.${entityName};
import ${sourceBasePackage}.service.${entityName}Service;

import java.util.List;

/**
 * ${description}(${entityName})控制类
 *
 * @author ${author}
 * @since ${createDate}
 */
@RestController
@Api(value = "${entityName}Api", tags = "${description}服务")
@RequestMapping(path = ${entityName}Api.PATH, produces = MediaType.APPLICATION_JSON_VALUE)
public class ${entityName}Controller extends BaseEntityController<${entityName}, ${entityName}Dto> implements ${entityName}Api {

	@Autowired
	private ${entityName}Service service;

	@Override
	public BaseEntityService<${entityName}> getService() {
	    return service;
	}

	@Override
	public ResultData<PageResult<${entityName}Dto>> findByPage(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {

		PageResult<${entityName}> pageResult = service.findPage(${entityName?uncap_first}SearchDto);

        ResultData<PageResult<${entityName}Dto>> result = this.convertToDtoPageResult(pageResult);

		return result;
	}

	@Override
    public ResultData<List<${entityName}Dto>> findByList(${entityName}SearchDto ${entityName?uncap_first}SearchDto) {

        List<${entityName}> list = service.findList(${entityName?uncap_first}SearchDto);

        List<${entityName}Dto> result = this.convertToDtos(list);

        return ResultData.success(result);
    }

	@Override
    public ResultData add(${entityName}Dto ${entityName?uncap_first}Dto) {

        try {
            service.add(${entityName?uncap_first}Dto);
            return ResultData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(e.getMessage());
        }
    }

    @Override
    public ResultData update(${entityName}Dto ${entityName?uncap_first}Dto) {

        try {
            service.update(${entityName?uncap_first}Dto);
            return ResultData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(e.getMessage());
        }
    }

    @Override
    public ResultData deleteById(${entityName}Dto ${entityName?uncap_first}Dto) {

        try {
            service.deleteById(${entityName?uncap_first}Dto.get${pkPropertyName?cap_first}());
            return ResultData.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.fail(e.getMessage());
        }
    }

}
